package com.molyfun.parents.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.listactivity.viewholder.ClassCourseListViewHolder;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.activity.store.view.ProductView;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.HorizonalTwoTabBar;
import com.molyfun.parents.view.TwoTabBar;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class MineCourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;

    private static final int MINE_CLASS_HEADER = 1001;

    private static final int MINE_CLASS_BODY = 1002;

    private static final int MINE_CLASS_TEAM_CLASS = 1003;

    private List<SubjectListResponse.SubjectBrief> mList;
    private List<CourseListResponse.CourseBean> mClassCourseList;

    private MineCourseListInterface mMineCourseListInterface;

    private int mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;

    public MineCourseAdapter(Context context, MineCourseListInterface mineCourseListInterface) {
        this.mContext = context;
        mMineCourseListInterface = mineCourseListInterface;
    }

    public interface MineCourseListInterface {
        void onTwoBarClick(int chooseTab);
        void onClassAdd();
        void onHistoryClick();
        void onDiscountsClick();
        void onProfileBtnClick();
        void onTeamClassClick();
    }

    public void refreshData(List<SubjectListResponse.SubjectBrief> list) {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
        this.mList = list;
        notifyDataSetChanged();
    }

    public void refreshClassCourseData(List<CourseListResponse.CourseBean> list) {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_TWO;
        this.mClassCourseList = list;
        notifyDataSetChanged();
    }

    public void clearAll(){
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
        if(mList != null){
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void clearAllClassCourse(){
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_TWO;
        if(mClassCourseList != null){
            mClassCourseList.clear();
        }
        notifyDataSetChanged();
    }

    public void addAll(List<SubjectListResponse.SubjectBrief> list) {
        if(mList != null){
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addAllClassCourse(List<CourseListResponse.CourseBean> list) {
        if(mClassCourseList != null){
            this.mClassCourseList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case MINE_CLASS_HEADER:
                return new MineClassHeaderViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_main_class_header, parent, false));
            case MINE_CLASS_TEAM_CLASS:
                return new ClassCourseListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_class_course_list_cell, parent, false));
            case MINE_CLASS_BODY:
            default:
                return new ProductBriefViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_product_cell, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(position == 0){
            MineClassHeaderViewHolder mineClassHeaderViewHolder = (MineClassHeaderViewHolder)holder;
            mineClassHeaderViewHolder.mAddClassBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMineCourseListInterface != null){
                        mMineCourseListInterface.onClassAdd();
                    }
                }
            });
            mineClassHeaderViewHolder.mHistoryLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMineCourseListInterface != null){
                        mMineCourseListInterface.onHistoryClick();
                    }
                }
            });
            mineClassHeaderViewHolder.mDiscountsLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMineCourseListInterface != null){
                        mMineCourseListInterface.onDiscountsClick();
                    }
                }
            });

            mineClassHeaderViewHolder.mProfileIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMineCourseListInterface != null){
                        mMineCourseListInterface.onProfileBtnClick();
                    }
                }
            });
            mineClassHeaderViewHolder.mHorizonalTwoTabBar.setTwoBarClickListener(new HorizonalTwoTabBar.TwoBarClickListener() {
                @Override
                public void onTwoBarClick(int chooseTab) {
                    if(mMineCourseListInterface != null){
                        mMineCourseListInterface.onTwoBarClick(chooseTab);
                    }
                }
            });

            mineClassHeaderViewHolder.mHorizonalTwoTabBar.initData("公开课", "班级课");

            //如果第一个tab是
            if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
                mineClassHeaderViewHolder.mAllUserTeamClassLayout.setVisibility(View.GONE);
            }else{
                mineClassHeaderViewHolder.mAllUserTeamClassLayout.setVisibility(View.VISIBLE);
                mineClassHeaderViewHolder.mTodayTime.setText(TimeUtils.getQueryDate());
                mineClassHeaderViewHolder.mAllUserTeamClassLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mMineCourseListInterface != null){
                            mMineCourseListInterface.onTeamClassClick();
                        }
                    }
                });
            }

            if(getItemCount() == 1){
                mineClassHeaderViewHolder.mNullClassLayout.setVisibility(View.VISIBLE);
                mineClassHeaderViewHolder.mAddClassTv.setVisibility(View.VISIBLE);
                if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
                    mineClassHeaderViewHolder.mAddClassTv.setText("添加课程");
                    mineClassHeaderViewHolder.mAddClassTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(mMineCourseListInterface != null){
                                mMineCourseListInterface.onClassAdd();
                            }
                        }
                    });
                    mineClassHeaderViewHolder.mNullClassTitle.setText("尚未添加任何课程");
                    mineClassHeaderViewHolder.mNullClassContent.setText("如果您想为孩子挑选适合他的专业课程，请点击下方文字添加相关课程。");
                }else{
                    mineClassHeaderViewHolder.mAddClassTv.setText("查看全部");
                    mineClassHeaderViewHolder.mAddClassTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(mMineCourseListInterface != null){
                                mMineCourseListInterface.onTeamClassClick();
                            }
                        }
                    });
                    mineClassHeaderViewHolder.mNullClassTitle.setText("老师尚未发布班级课");
                    mineClassHeaderViewHolder.mNullClassContent.setText("加入班级后老师会发布班级课，发布后会展示当天的班级课，可以点击查看全部查看。");
                }
            }else{
                mineClassHeaderViewHolder.mNullClassLayout.setVisibility(View.GONE);
            }

        }else{
            switch (getItemViewType(position)){
                case MINE_CLASS_TEAM_CLASS:
                    final CourseListResponse.CourseBean courseBean = mClassCourseList.get(position - 1);
                    ClassCourseListViewHolder courseListViewHolder = (ClassCourseListViewHolder)holder;
                    courseListViewHolder.mCourseView.initData(courseBean);
                    courseListViewHolder.mCourseView.setPadding(mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0, mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0);
                    break;
                case MINE_CLASS_BODY:
                    final SubjectListResponse.SubjectBrief productBean = mList.get(position - 1);
                    ProductBriefViewHolder productBriefViewHolder = (ProductBriefViewHolder)holder;
                    productBriefViewHolder.mProductView.initData(productBean);
                    productBriefViewHolder.mProductView.setPadding(mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0, mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0);
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return MINE_CLASS_HEADER;
        }else{
            if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
                return MINE_CLASS_BODY;
            }else{
                return MINE_CLASS_TEAM_CLASS;
            }
        }
    }

    public class ProductBriefViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.product_layout)
        ProductView mProductView;

        public ProductBriefViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class MineClassHeaderViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.add_class_btn)
        ImageView mAddClassBtn;
        @InjectView(R.id.history_layout)
        ImageView mHistoryLayout;
        @InjectView(R.id.discounts_layout)
        ImageView mDiscountsLayout;
        @InjectView(R.id.profile_icon)
        ImageView mProfileIcon;
        @InjectView(R.id.two_tab_bar)
        HorizonalTwoTabBar mHorizonalTwoTabBar;
        @InjectView(R.id.null_class_layout)
        LinearLayout mNullClassLayout;
        @InjectView(R.id.null_class_title)
        TextView mNullClassTitle;
        @InjectView(R.id.null_class_content)
        TextView mNullClassContent;
        @InjectView(R.id.add_class)
        TextView mAddClassTv;
        @InjectView(R.id.user_team_class_all_layout)
        LinearLayout mAllUserTeamClassLayout;
        @InjectView(R.id.today_time)
        TextView mTodayTime;


        public MineClassHeaderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getItemCount() {

        if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
            if (mList == null || mList.size() == 0) {
                return 1;
            }
            return mList.size() + 1;
        }else{
            if (mClassCourseList == null || mClassCourseList.size() == 0) {
                return 1;
            }
            return mClassCourseList.size() + 1;
        }
    }

}
